﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BHXH.Frm.FrmUserControl;
using Infragistics.Win.UltraWinTabControl;
using BHXH.Core.IService;
using FX.Core;
using BHXH.Core.Domain;
using Infragistics.Win.UltraWinGrid;
using Infragistics.Win.UltraWinTree;
using Infragistics.Win;

namespace BHXH.Frm.Frm
{
    public partial class DangKyBHXH : UserControl
    {
        Dkbhxh_DanhSach usercontrol_ds;
        A01a_TS usercontrol_A01ATS;
        D02_TS usercontrol_D02TS;
        D02b_TS usercontrol_D02bTS;
        C15a_TS usercontrol_C15aTS;
        A01_TS usercontrol_A01TS;
        D01b_TS usercontrol_D01bTS;
        _01_DKBB usercontrol_01DKBB;
        Dinh_Kem usercontrol_DinhKem;
        private readonly Ihs_A01aTS_dtService _hs_01adtSrv;
        int ID_Ref = 0;
        #region get_set_Ultratabpage
        public UltraTabPageControl A01aTS_TabPage
        {
            get { return ultraTabPage_A01aTS; }
            set { ultraTabPage_A01aTS = value; }
        }
        public UltraTabPageControl D02TS_TabPage
        {
            get { return ultraTabPage_D02TS; }
            set { ultraTabPage_D02TS = value; }
        }
        public UltraTabPageControl D02bTS_TabPage
        {
            get { return ultraTabPage_D02bTS; }
            set { ultraTabPage_D02bTS = value; }
        }
        public UltraTabPageControl C15aTS_TabPage
        {
            get { return ultraTabPage_C15aTS; }
            set { ultraTabPage_C15aTS = value; }
        }
        public UltraTabPageControl A01TS_TabPage
        {
            get { return ultraTabPage_A01TS; }
            set { ultraTabPage_A01TS = value; }
        }
        public UltraTabPageControl D01bTS_TabPage
        {
            get { return ultraTabPage_D01bTS; }
            set { ultraTabPage_D01bTS = value; }
        }
        public UltraTabPageControl _01DKBB_TabPage
        {
            get { return ultraTabPage_01DKBB; }
            set { ultraTabPage_01DKBB = value; }
        }
        public UltraTabPageControl DinhKem_TabPage
        {
            get { return ultraTabPage_DinhKem; }
            set { ultraTabPage_DinhKem = value; }
        }
        #endregion

        public DangKyBHXH()
        {
            InitializeComponent();
            _hs_01adtSrv = IoC.Resolve<Ihs_A01aTS_dtService>();
            Add_TreeNode();

            usercontrol_ds = new Dkbhxh_DanhSach();
            showControl_TabPage_Danhsach(usercontrol_ds);
            usercontrol_ds.checkIfFalse += new Dkbhxh_DanhSach.customHandler(SetDataFromChild_False);
            usercontrol_ds.checkIfTrue += new Dkbhxh_DanhSach.customHandler(SetDataFromChild_True);

            usercontrol_A01ATS = new A01a_TS();
            showControl_TabPage_A01ats(usercontrol_A01ATS);

            usercontrol_D02TS = new D02_TS();
            showControl_TabPage_D02ts(usercontrol_D02TS);

            usercontrol_D02bTS = new D02b_TS();
            showControl_TabPage_D02bts(usercontrol_D02bTS);

            usercontrol_C15aTS = new C15a_TS();
            showControl_TabPage_C15ats(usercontrol_C15aTS);

            usercontrol_A01TS = new A01_TS();
            showControl_TabPage_A01ts(usercontrol_A01TS);

            usercontrol_D01bTS = new D01b_TS();
            showControl_TabPage_D01bts(usercontrol_D01bTS);

            usercontrol_01DKBB = new _01_DKBB();
            showControl_TabPage_01dkbb(usercontrol_01DKBB);

            usercontrol_DinhKem = new Dinh_Kem();
            showControl_TabPage_Dinhkem(usercontrol_DinhKem);

            Hide_TabControl();
            usercontrol_ds.Hide_Soluong();
        }

        void Add_TreeNode()
        {
            treeView1.Nodes.Add("Tháng " + DateTime.Now.Month.ToString());
            treeView1.Nodes[0].Checked = true;
            btnThemTree.Visible = true;
            btnSuaTree.Visible = false;
            btnXoaTree.Enabled = false;            
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (treeView1.Nodes[0].Nodes.Count > 0)
            {
                if (treeView1.Nodes[0].IsSelected)
                {
                    ultraTabControl1.Enabled = false;
                    btnThemTree.Visible = true;
                    btnSuaTree.Visible = false;
                    btnXoaTree.Enabled = false; 
                }
                else
                {
                    ultraTabControl1.Enabled = true;
                    MessageBox.Show("Select * from hoso where ID_Ref = "+ID_Ref.ToString());
                    btnThemTree.Visible = false;
                    btnSuaTree.Visible = true;
                    btnXoaTree.Enabled = true; 
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {            
            TreeNode child = treeView1.Nodes[0].Nodes.Add(ID_Ref.ToString(),txtTreeName.Text);            
            ID_Ref += 1;
            treeView1.SelectedNode = child;
            treeView1.ExpandAll();
            btnThemTree.Visible = false;
            btnSuaTree.Visible = true;
            btnXoaTree.Enabled = true;            
        }

        private void btnXoaTree_Click(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode != null)
            {
                treeView1.Nodes.Remove(treeView1.SelectedNode);
                treeView1.SelectedNode = treeView1.Nodes[0];
                btnThemTree.Visible = true;
                btnSuaTree.Visible = false;
                btnXoaTree.Enabled = false;
                ID_Ref -= 1;
            }            
        }
        /// <summary>
        /// Ẩn các tabpagecontrol khi gọi từ form con DKBHXH_DanhSach
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="i"></param>
        void SetDataFromChild_False(object sender, string i)
        {
            switch (i)
            {
                case "a01ats":
                    A01aTS_TabPage.Tab.Visible = false;
                    break;
                case "a01ts":
                    A01TS_TabPage.Tab.Visible = false;
                    break;
                case "c15ats":
                    C15aTS_TabPage.Tab.Visible = false;
                    break;
                case "d01bts":
                    D01bTS_TabPage.Tab.Visible = false;
                    break;
                case "d02bts":
                    D02bTS_TabPage.Tab.Visible = false;
                    break;
            }

        }
        /// <summary>
        /// Hiện các tabpagecontrol khi gọi từ form con DKBHXH_DanhSach
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="i"></param>
        void SetDataFromChild_True(object sender, string i)
        {
            switch (i)
            {
                case "a01ats":
                    A01aTS_TabPage.Tab.Visible = true;
                    break;
                case "a01ts":
                    A01TS_TabPage.Tab.Visible = true;
                    break;
                case "c15ats":
                    C15aTS_TabPage.Tab.Visible = true;
                    break;
                case "d01bts":
                    D01bTS_TabPage.Tab.Visible = true;
                    break;
                case "d02bts":
                    D02bTS_TabPage.Tab.Visible = true;
                    break;
            }
        }

        #region fill_usercontrol

        public void showControl_TabPage_Danhsach(System.Windows.Forms.Control obj)
        {
            panel_danhsach.Controls.Clear();
            obj.Dock = DockStyle.Fill;
            panel_danhsach.Controls.Add(obj);
            panel_danhsach.Controls[obj.Name].Focus();
        }
        public void showControl_TabPage_A01ats(System.Windows.Forms.Control obj)
        {
            panel_A01ats.Controls.Clear();
            obj.Dock = DockStyle.Fill;
            panel_A01ats.Controls.Add(obj);
            panel_A01ats.Controls[obj.Name].Focus();
        }
        public void showControl_TabPage_D02ts(System.Windows.Forms.Control obj)
        {
            panel_D02ts.Controls.Clear();
            obj.Dock = DockStyle.Fill;
            panel_D02ts.Controls.Add(obj);
            panel_D02ts.Controls[obj.Name].Focus();
        }
        public void showControl_TabPage_D02bts(System.Windows.Forms.Control obj)
        {
            panel_d02bts.Controls.Clear();
            obj.Dock = DockStyle.Fill;
            panel_d02bts.Controls.Add(obj);
            panel_d02bts.Controls[obj.Name].Focus();
        }
        public void showControl_TabPage_C15ats(System.Windows.Forms.Control obj)
        {
            panel_c15ats.Controls.Clear();
            obj.Dock = DockStyle.Fill;
            panel_c15ats.Controls.Add(obj);
            panel_c15ats.Controls[obj.Name].Focus();
        }
        public void showControl_TabPage_A01ts(System.Windows.Forms.Control obj)
        {
            panel_a01ts.Controls.Clear();
            obj.Dock = DockStyle.Fill;
            panel_a01ts.Controls.Add(obj);
            panel_a01ts.Controls[obj.Name].Focus();
        }
        public void showControl_TabPage_D01bts(System.Windows.Forms.Control obj)
        {
            panel_d01bts.Controls.Clear();
            obj.Dock = DockStyle.Fill;
            panel_d01bts.Controls.Add(obj);
            panel_d01bts.Controls[obj.Name].Focus();
        }
        public void showControl_TabPage_01dkbb(System.Windows.Forms.Control obj)
        {
            panel_01dkbb.Controls.Clear();
            obj.Dock = DockStyle.Fill;
            panel_01dkbb.Controls.Add(obj);
            panel_01dkbb.Controls[obj.Name].Focus();
        }
        public void showControl_TabPage_Dinhkem(System.Windows.Forms.Control obj)
        {
            panel_Dinhkem.Controls.Clear();
            obj.Dock = DockStyle.Fill;
            panel_Dinhkem.Controls.Add(obj);
            panel_Dinhkem.Controls[obj.Name].Focus();
        }
        #endregion
        /// <summary>
        /// Thoát ứng dụng
        /// </summary>
        void Exit_App()
        {
            if (DialogResult.Yes == MessageBox.Show("Bạn có chắc chắn thoát không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
            {
                Application.ExitThread();
            }
        }
        /// <summary>
        /// Ẩn các tabcontrol ngay khi load form
        /// </summary>
        void Hide_TabControl()
        {
            D02TS_TabPage.Tab.Visible = true;
            _01DKBB_TabPage.Tab.Visible = true;
            DinhKem_TabPage.Tab.Visible = true;
            A01aTS_TabPage.Tab.Visible = false;
            A01TS_TabPage.Tab.Visible = false;
            C15aTS_TabPage.Tab.Visible = false;
            D01bTS_TabPage.Tab.Visible = false;
            D02bTS_TabPage.Tab.Visible = false;
        }

        private void ultraTabControl1_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.F3:
                    if (A01aTS_TabPage.Tab.Visible == true)
                    {
                        usercontrol_A01ATS.Xoa_A01aTS();
                    }
                    if (C15aTS_TabPage.Tab.Visible == true)
                    {
                        usercontrol_C15aTS.Xoa_C15aTS();
                    }
                    if (A01TS_TabPage.Tab.Visible == true)
                    {
                        usercontrol_A01TS.Xoa_A01TS();
                    }
                    if (D02bTS_TabPage.Tab.Visible == true)
                    {
                        usercontrol_D02bTS.Xoa_D02bTS();
                    }
                    break;
                case Keys.F5:
                    if (A01aTS_TabPage.Tab.Visible == true)
                    {
                        usercontrol_A01ATS.Ghi_A01aTS();
                    }
                    if (C15aTS_TabPage.Tab.Visible == true)
                    {
                        usercontrol_C15aTS.Ghi_C15aTS();
                    }
                    if (A01TS_TabPage.Tab.Visible == true)
                    {
                        usercontrol_A01TS.Ghi_A01TS();
                    }
                    if (_01DKBB_TabPage.Tab.Visible == true)
                    {
                        usercontrol_01DKBB.NhapLai_01_DKBB();
                    }
                    if (D02bTS_TabPage.Tab.Visible == true)
                    {
                        usercontrol_D02bTS.Ghi_D02bTS();
                    }
                    if (_01DKBB_TabPage.Tab.Visible == true)
                    {
                        usercontrol_01DKBB.Ghi_01_DKBB();
                    }
                    if (D01bTS_TabPage.Tab.Visible == true)
                    {
                        usercontrol_D01bTS.Ghi_D01B_TS();
                    }
                    if (DinhKem_TabPage.Tab.Visible == true)
                    {
                        usercontrol_DinhKem.Ghi_DinhKem();
                    }
                    break;

                case Keys.Escape:
                    if (_01DKBB_TabPage.Tab.Visible == true)
                    {
                        usercontrol_01DKBB.NhapLai_01_DKBB();
                    }
                    if (D01bTS_TabPage.Tab.Visible == true)
                    {
                        usercontrol_D01bTS.NhapLai_D01B_TS();
                    }
                    break;
                case Keys.F12:
                    Exit_App();
                    break;
            }
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            Exit_App();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (A01aTS_TabPage.Tab.Visible == true)
            {
                usercontrol_A01ATS.Xoa_A01aTS();
            }
            if (C15aTS_TabPage.Tab.Visible == true)
            {
                usercontrol_C15aTS.Xoa_C15aTS();
            }
            if (A01TS_TabPage.Tab.Visible == true)
            {
                usercontrol_A01TS.Xoa_A01TS();
            }
            if (D02bTS_TabPage.Tab.Visible == true)
            {
                usercontrol_D02bTS.Xoa_D02bTS();
            }
        }

        private void btnGhi_Click(object sender, EventArgs e)
        {
            if (A01aTS_TabPage.Tab.Visible == true)
            {
                usercontrol_A01ATS.Ghi_A01aTS();
            }
            if (C15aTS_TabPage.Tab.Visible == true)
            {
                usercontrol_C15aTS.Ghi_C15aTS();
            }
            if (A01TS_TabPage.Tab.Visible == true)
            {
                usercontrol_A01TS.Ghi_A01TS();
            }
            if (D02bTS_TabPage.Tab.Visible == true)
            {
                usercontrol_D02bTS.Ghi_D02bTS();
            }
            if (_01DKBB_TabPage.Tab.Visible == true)
            {
                usercontrol_01DKBB.Ghi_01_DKBB();
            }
            if (D01bTS_TabPage.Tab.Visible == true)
            {
                usercontrol_D01bTS.Ghi_D01B_TS();
            }
            if (DinhKem_TabPage.Tab.Visible == true)
            {
                usercontrol_DinhKem.Ghi_DinhKem();
            }
        }

        private void btnNhapLai_Click(object sender, EventArgs e)
        {
            if (_01DKBB_TabPage.Tab.Visible == true)
            {
                usercontrol_01DKBB.NhapLai_01_DKBB();
            }
            if (D01bTS_TabPage.Tab.Visible == true)
            {
                usercontrol_D01bTS.NhapLai_D01B_TS();
            }
        }

    }
}
